package com.lkm.app_ui.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.AppUIHelp;
import com.lkm.app_ui.m.fun.OnKeyNumberEvent;
import com.lkm.app_ui.m.goods.BarcodeResultBO;
import com.lkm.app_ui.m.goods.GoodsEntity;
import com.lkm.app_ui.m.goods.GoodsModule;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.util.CycleHelp;
import com.lkm.javatool.CollectionHelp;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;



public class FindBarcodeResultFragment extends MBaseFragment {


    public static FindBarcodeResultFragment getInstance() {
        FindBarcodeResultFragment f = new FindBarcodeResultFragment();





        return f;
    }

    public static FindBarcodeResultFragment instance;


    public FindBarcodeResultFragment() {
        instance = this;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_find_barcode_result;
    }

    @Override
    protected boolean configIsLockKey() {
        return true;
    }

    private String name1 = "";


    private BarcodeResultBO barcodeResultBO;

    TextView tv_name1, tv_code;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        interceptedKeyAndNumberEvent();

        tv_name1 = ((TextView) findViewById(R.id.tv_name1));//.setText(weightChangeInfo.getName());
        tv_code = ((TextView) findViewById(R.id.tv_code));//.setText(SteelardHelp.formatMoney(weightChangeInfo.getUnitPrice()));

        String s = "确认商品：'商品提取'按钮\n\n取消：'功能选择'按钮\n\n删除输入：'.'按钮\n\n清空输入：'单价清除'按钮";

        GoodsModule goodsModule = (GoodsModule)AppConst.getModuleManager().peekModule(GoodsModule.class);

        if(goodsModule!=null)
            s += "\n\n本地商品数："+goodsModule.getGoodsSize();


        ((TextView) findViewById(R.id.tv_tips)).setText(s);

        AppUIHelp.inputUI(tv_code);
    }


    private void onBarcodeResultBO() {
        if (barcodeResultBO == null) return;

//        unitPrice1 = StringHelp.toFloat(barcodeResultBO.price, unitPrice1);
        name1 = "";
        if (barcodeResultBO.goodsEntity != null) {
            name1 = barcodeResultBO.goodsEntity.name;
        } else if (!CollectionHelp.isEmpty(barcodeResultBO.goodsEntities)) {
            name1 = barcodeResultBO.goodsEntities.get(0).name;
        }


        tv_name1.setText(name1);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();


        if (instance == this)
            instance = null;

    }


    private void onCodeWord(String codeWord) {
        tv_code.setText(codeWord);

        GoodsModule goodsModule = (GoodsModule) AppConst.getModuleManager().peekModule(GoodsModule.class);
        if (goodsModule != null) {
            barcodeResultBO = goodsModule.findBarcodeResult(codeWord);
            if (barcodeResultBO != null) {
                onBarcodeResultBO();
            }
        }
    }

    private void interceptedKeyAndNumberEvent() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnSteelyardKeyEvent>() {
            @Override
            public Class<OnSteelyardKeyEvent> getBusEventClass() {
                return OnSteelyardKeyEvent.class;
            }

            String codeWord = "";

            @Override
            public void onReceiver(OnSteelyardKeyEvent data) {
                if (!data.isIntercepted()) {
                    data.setIntercepted(true);
                    if (data.getKeyCode() <= OnSteelyardKeyEvent.KEYCODE_9) {

                        codeWord = codeWord + data.getKeyCode();
                        onCodeWord(codeWord);
                    } else if (data.getKeyCode() <= OnSteelyardKeyEvent.KEYCODE_DOT) {

                        if (codeWord.length() > 0) {
                            codeWord = codeWord.substring(0, codeWord.length() - 1);
                            onCodeWord(codeWord);
                        }
                    }else if (data.getKeyCode() <= OnSteelyardKeyEvent.KEYCODE_CLEAR_PRICE) {

                        codeWord = "";
                        onCodeWord(codeWord);
                    }
                    if (data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_FUN) {
                        RouterUINavigation.getInstance().dismiss(FindBarcodeResultFragment.this);
                    } else if (data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_GOODTIQU) {


                        GoodsEntity goodsEntity = null;

                        if (barcodeResultBO != null) {

                            if (barcodeResultBO.goodsEntity == null) {
                                barcodeResultBO.goodsEntity = CollectionHelp.getFirst(barcodeResultBO.goodsEntities);
                            }
                            goodsEntity = barcodeResultBO.goodsEntity;
                        }

                        if (goodsEntity == null) {
                            AppUIHelp.tryShowTipsSHORT( "无对应商品可提取");
                            return;
                        }

                        RunTimeData.barcodeResultBO = barcodeResultBO;
                        RouterUINavigation.getInstance().dismiss(FindBarcodeResultFragment.this);
                        if (RunTimeData.barcodeResultBO.goodsEntity.type == GoodsEntity.Type_countNum) {
                            RouterUINavigation.getInstance().showCountNum(true);
                        } else {
                            RouterUINavigation.getInstance().showBarcodeResult();
                        }
                    }
                }

            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);



        LinkBusCycleWrapper.register(new ObserverAutoNext<OnKeyNumberEvent>() {
            @Override
            public Class<OnKeyNumberEvent> getBusEventClass() {
                return OnKeyNumberEvent.class;
            }

            boolean isOnce = false;

            @Override
            public void onReceiver(OnKeyNumberEvent data) {
                if (!data.isIntercepted()) {
                    data.setIntercepted(true);
                    if (!isOnce && data.isFinally()) {

                        isOnce = true;
                        return;
                    }
                    isOnce = true;
                }
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }


}
